<template>
  <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item label="新密码" prop="password">
      <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm">提交</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
  export default {
    data() {
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入新密码'));
        }  else {
          callback();
        }
      };
      return {
        ruleForm: {
          id: Number(sessionStorage.getItem("id")),
          password: null,
          role: "USER",
          aclock: 0,
          clockin: 0,
        },
        rules: {
          password: [
            { validator: validatePass2, trigger: 'blur' }
          ],
        }
      };
    },
    created() {
      this.initPage();
    },
    methods: {
      initPage(){
        this.$axios.get('/account/accheck').then()
          .catch(() => {
            this.$router.replace({path: '/'});
            location.reload();
          });
      },
      submitForm() {
        this.$axios({
          method: "post",
          url: "/account/update",
          headers: {
            "Content-Type": "application/json"  //传json数据要加这个，不然导致后端接收的数据与前端不一致
          },
          data: this.ruleForm,
        }).then(()=>{
          window.alert("修改成功!");
        }).catch((error) => {
          window.alert(error);
        });
      },
    }
  }
</script>

<style scoped>

</style>
